package com.example.administrator.commonlibrary.definedview.extension;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.example.administrator.commonlibrary.bese.BaseFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;


/**
 * @author wang_hong_wei
 * @description 自定义ViewPager类 实现Viewpager不可滑动
 * @Date 2018-6-9
 */
public class ControlScrollViewPager extends ViewPager {
    private List<BaseFragment> fragments = new ArrayList<>();
    private boolean isCanScroll = true;

    public ControlScrollViewPager(Context context) {
        this(context, null);
    }

    public ControlScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);

    }


    /**
     * @param isCanScroll 是否支持滑动
     * @desc
     */
    public void setScanScroll(boolean isCanScroll) {
        this.isCanScroll = isCanScroll;

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (!isCanScroll) {
            return false;
        }
        return super.onInterceptHoverEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (!isCanScroll) {
            return false;
        }
        return super.onTouchEvent(ev);

    }


    public void setAdapter(FragmentManager fm, boolean isCanScroll, List<BaseFragment> fragments, final CommonTabLayout tabLayout) {

        this.isCanScroll = isCanScroll;
        this.fragments.clear();
        this.fragments.addAll(fragments);
        setAdapter(new MyPagerAdapter(fm));
        setOffscreenPageLimit(fragments.size());
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        setCurrentItem(0);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int position) {

            return fragments.get(position);
        }
    }


    @Override
    public void setCurrentItem(int item) {
        //去除页面切换时的滑动翻页效果
        super.setCurrentItem(item, false);
    }
}
